%MONTE_CARLO_ESTIMATION
%INPUT:
%   bottle_shape
%   liquid_level
%   mode_parameters
%   mode
function estimation = monte_carlo_estimation(bottle_shape, liquid_level, mode_parameters, mode)

points_number = mode_parameters.monte_carlo_points_number;

bound = [min(bottle_shape(1,:)) max(bottle_shape(1,:)) ...
         min(bottle_shape(2,:)) max(bottle_shape(2,:))];

level = liquid_level;
if (bound(4) < level)
    level = bound(4);
end
if (bound(3) > level)
    level = bound(3);
end
%if (bound(4) < liquid_level || liquid_level < bound(3))
%    error('Invalid liquid level');
%end

random_points = rand(points_number, 2);


random_points(:,1) = (bound(2) - bound(1)) * random_points(:,1) + bound(1);
random_points(:,2) = (level - bound(3)) * random_points(:,2) + bound(3);
IN = inpolygon(random_points(:,1), random_points(:,2), bottle_shape(1,:),...
                                                       bottle_shape(2,:));
whole_volum = (bound(2) - bound(1)) * (liquid_level - bound(3));
points_in_area = sum(sum(IN));

switch mode
    case 'volume'
        estimation = whole_volum * points_in_area ./ points_number;
    case 'mass_center'
        estimation = (sum(random_points(IN,:))./ points_in_area)';
    otherwise
        error('Invalid mode');
end

